﻿using DotNetNuke.Entities.Modules;
using DotNetNuke.Security.Permissions;
using HyperCrunch.Druid.Mobile;
using System;

/*******************************************************************************
 IMPORTANT: Priot to release, make sure this class file has a "Build Action" of 
            "Content" in Visual Studio to ensure that the module does not have 
            a dependency on HyperCrunch.Druid or HyperCrunch.Druid.Mobile 
  
            Also remove all project references to HyperCrunch.Druid and 
            HyperCrunch.Druid.Mobile prior to release.
*******************************************************************************/
 
namespace HyperCrunch.Druid.Sample 
{
    public class AppController : AppControllerBase, IMobileAppController
    {
        /// <summary>
        /// Called when displaying the initial app view
        /// </summary>
        /// <returns>Model used by Controller to render View</returns>
        public IMobileAppModel GetViewModel(ModuleInfo moduleInfo, 
                                            UIContext uiContext, 
                                            string returnUrl)
        {
            AppModel model = new AppModel();
            model.AppContext = uiContext;
            model.Module = moduleInfo;
            model.Products = ProductModel.GetProducts();
            model.Title = moduleInfo.ModuleTitle;
            return model;
        }

        /// <summary>
        /// Called using jQuery AJAX POST to display details of
        /// the selected product
        /// </summary>
        /// <returns>Product object in JSON format</returns>
        [RequestMethod(HttpVerbType.POST)] 
        public JsonResult Detail(string id)
        {
            return new JsonResult 
                            { 
                                Data = ProductModel.GetProduct(id), 
                                Serialize = true
                            };
        }

        /// <summary>
        /// Called using jQuery AJAX POST to update a Product
        /// </summary>
        /// <returns>Empty string for success or ErrorResult on error</returns>
        [RequestMethod(HttpVerbType.POST)]
        public ActionResult Update( ModuleInfo moduleInfo, 
                                    UIContext uiContext, 
                                    string id, 
                                    string description)
        {
            if (!ModulePermissionController.CanEditModuleContent(moduleInfo))
                return new ErrorResult { Content = "You cannot edit this content" };

            try
            {
                Product product = ProductModel.GetProduct(id);
                if (product != null)
                {
                    product.Description = description;
                    ProductModel.UpdateProduct(id, product);
                    return new ContentResult { Content = "" };
                }
                else
                    return new ErrorResult { Content = "Product not found" };
            }
            catch (Exception exc)
            {
                return new ErrorResult { Content = exc.Message };
            }
        }

    }
}